﻿using Jay.Sc2.Bof.Lib.State;

namespace Jay.Sc2.Bof.Lib.Action.Protoss
{
	public abstract class ActionProduceFromRoboticsFacility : ActionProduceProtoss
	{
		protected ActionProduceFromRoboticsFacility(int buildTime, int minerals, int gas, int food) : base(buildTime, minerals, gas, food) { }

		public override int AvailableProducer(IBuildOrder bo)
		{
			ProtossState state = bo.State as ProtossState;
			return state.RoboticsFacility.AvailableForProduction;
		}

		public abstract string EventName { get; }
		public abstract void OnProductionFinished(IBuildOrder bo);

		public override bool HaveTechnology(IBuildOrder bo)
		{
			ProtossState state = bo.State as ProtossState;
			return state.RoboticsFacility.Count > 0;
		}

		public override bool IsInvalid(IBuildOrder bo)
		{
			ProtossState state = bo.State as ProtossState;
			return state.RoboticsFacility.Total == 0 || base.IsInvalid(bo);
		}

		public override void Execute(IBuildOrder bo)
		{
			ProtossState state = bo.State as ProtossState;
			base.Execute(bo);
			state.RoboticsFacility.Production++;
			bo.AddAction(state.Time + BuildTime, new ActionGeneric(EventName, (ActionGeneric.ActionExecuteDelegate)delegate(IBuildOrder ibo)
			{
				ProtossState ibostate = ibo.State as ProtossState;
				ibostate.RoboticsFacility.Production--;
				OnProductionFinished(ibo);
			}));
		}

		protected IAction[,] m_Requirements;
		public override IAction[,] Requirements
		{
			get
			{
				if (m_Requirements == null)
					m_Requirements = new IAction[,] { { ActionProduceRoboticsFacility.Instance } };
				return m_Requirements;
			}
		}
	}

	public class ActionProduceImmortal : ActionProduceFromRoboticsFacility
	{
		public static ActionProduceImmortal Instance = new ActionProduceImmortal();
		private ActionProduceImmortal() : base(55, 250, 100, 4) { }
		public override string EventName { get { return "ActionSpawnImmortal"; } }
		public override void OnProductionFinished(IBuildOrder bo)
		{
			ProtossState state = bo.State as ProtossState;
			state.Immortal++;
		}
	}
	public class ActionProduceObserver : ActionProduceFromRoboticsFacility
	{
		public static ActionProduceObserver Instance = new ActionProduceObserver();
		private ActionProduceObserver() : base(40, 50, 100, 1) { }
		public override string EventName { get { return "ActionSpawnObserver"; } }
		public override void OnProductionFinished(IBuildOrder bo)
		{
			ProtossState state = bo.State as ProtossState;
			state.Observer++;
		}
	}
	public class ActionProduceWarpPrism : ActionProduceFromRoboticsFacility
	{
		public static ActionProduceWarpPrism Instance = new ActionProduceWarpPrism();
		private ActionProduceWarpPrism() : base(50, 200, 0, 2) { }
		public override string EventName { get { return "ActionSpawnWarpPrism"; } }
		public override void OnProductionFinished(IBuildOrder bo)
		{
			ProtossState state = bo.State as ProtossState;
			state.WarpPrism++;
		}
	}
	public class ActionProduceColossus : ActionProduceFromRoboticsFacility
	{
		public static ActionProduceColossus Instance = new ActionProduceColossus();
		private ActionProduceColossus() : base(75, 300, 200, 6) { }
		public override string EventName { get { return "ActionSpawnColossus"; } }
		public override void OnProductionFinished(IBuildOrder bo)
		{
			ProtossState state = bo.State as ProtossState;
			state.Colossus++;
		}
		public override bool HaveTechnology(IBuildOrder bo)
		{
			ProtossState state = bo.State as ProtossState;
			return base.HaveTechnology(bo) && state.RoboticsBay.Count > 0;
		}
		public override bool IsInvalid(IBuildOrder bo)
		{
			ProtossState state = bo.State as ProtossState;
			return base.IsInvalid(bo) || state.RoboticsBay.Total == 0;
		}
		public override IAction[,] Requirements
		{
			get
			{
				if (m_Requirements == null)
					m_Requirements = new IAction[,] { { ActionProduceRoboticsBay.Instance } };
				return base.Requirements;
			}
		}
	}
}
